package com.example.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.example.entity.Advertisement;
import com.example.mapp.AdvertisementMapp;
import com.example.service.AdvertisementService;
import com.example.util.ResultUtil;

import ch.qos.logback.classic.Logger;
import org.springframework.stereotype.Service;

/**
 * Created by douyx on 2018/03/26.
 */
@Service
public class AdvertisementServiceImpl implements AdvertisementService {

	private final static Logger logger = (Logger) LoggerFactory.getLogger(AdvertisementServiceImpl.class);

	@Autowired
	private AdvertisementMapp advertisementMapp;

	@Override
	public Map<String, Object> query(Map map) {
		try {
			List<Advertisement> advertisementList = advertisementMapp.query(map);
			Map a = new HashMap();
			a.put("list",advertisementList);
			return ResultUtil.returnSuccess("查询成功", a);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return ResultUtil.returnError("查询失败" + e.getMessage(), null);
		}
	}

	@Override
	public Map<String, Object> queryAll() {
		try {
			List<Advertisement> advertisementList = advertisementMapp.queryAll();
			Map a = new HashMap();
			a.put("list",advertisementList);
			return ResultUtil.returnSuccess("查询成功", a);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return ResultUtil.returnError("查询失败" + e.getMessage(), null);
		}
	}

	@Override
	public Map<String, Object> add(Advertisement advertisement) {
		try {
			advertisementMapp.add(advertisement);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return ResultUtil.returnError("提交失败" + e.getMessage(), null);
		}
		return ResultUtil.returnSuccess("提交成功", null);
	}

	/**
	 * 修改可删除多条记录
	 * @param ids
	 * @return
	 */
	@Override
	public Map<String, Object> delete(List<String> ids) {
		try {
			for(String id : ids){
				advertisementMapp.delete(id);
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			return ResultUtil.returnError("提交失败" + e.getMessage(), null);
		}
		return ResultUtil.returnSuccess("提交成功", null);
	}

	@Override
	public Map<String, Object> upDate(Advertisement advertisement) {
		try {
			advertisementMapp.upDate(advertisement);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return ResultUtil.returnError("更新失败" + e.getMessage(), null);
		}
		return ResultUtil.returnSuccess("更新成功", null);
	}

	@Override
	public Map<String, Object> queryById(Advertisement advertisement) {
		try {
			Advertisement advertisement1 = advertisementMapp.queryById(advertisement);
			Map map1 = new HashMap();
			map1.put("advertisement",advertisement1);
			return ResultUtil.returnSuccess("查询成功",map1);
		} catch (Exception e) {
			logger.error(e.getMessage());
			return ResultUtil.returnError("查询失败" + e.getMessage(), null);
		}
	}
}
